<?php
	
	/**
	 * PkMailer File.
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package pink-koala
	 */
	
	Abstract Class Mailer
	{
		public static function send($class, $action, $args = array())
		{
			if(! preg_match("/Mailer$/", $class))
			{
				$class .= "Mailer";
			}
			
			$dir = preg_replace("/_mailer$/", "", uncamelize($class));
			
			$email = new $class("default", $dir . DS . $action);
			$email->$action($args);
			$email->send();
		}
		
		public static function preview($class, $action, $args = array())
		{
			if(! preg_match("/Mailer$/", $class))
			{
				$class .= "Mailer";
			}
			
			$dir = preg_replace("/_mailer$/", "", uncamelize($class));
			
			$email = new $class("default", $dir . DS . $action);
			$email->$action($args);
			return $email->preview();
		}
		
	}
	
	
	
	/**
	 * PkMailer class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 **/
	
	Abstract Class PkMailer
	{
		public $layout;
		public $email_template;
		public $to;
		public $from;
		
		public $subject;
		public $txt_body;
		public $html_body;
		
		public $txt_preview;
		public $html_preview;
		
		private $is_rendered = false;
		private $connection = false;
		
		/**
		 * Default Constructor for PkMailer
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		public function __construct($layout = "default", $template = "default")
		{
			$this->layout = $layout;
			$this->email_template = $template;
			
			$this->to = array();
			$this->from = array();
			
			$this->subject = "";
			$this->txt_body = "";
			$this->html_body = "";
			
			$this->txt_preview = "";
			$this->html_preview = "";
		}
		
		
		/**
		 * render function
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		private function render()
		{
			// get the file names
			$filename = PROJECT_DIR . 'app/views/email/' . $this->email_template;
			$html_file = $filename . '.html';
			$txt_file = $filename . '.txt';
			
			$layout_filename = PROJECT_DIR . 'app/views/email/layouts/' . $this->layout;
			$html_layout_file = $layout_filename . '.html';
			$txt_layout_file = $layout_filename . '.txt';
			
			//
			// txt
			//
			
			// template
			ob_start();
			require($txt_file);
			$body = ob_get_contents();
			ob_end_clean();
			
			// subject
			$this->subject = substr($body, 0, strpos($body, "\n"));
			
			// get rid of the subject from the body
			$body = substr_replace($body, "", 0, strpos($body, "\n") + 1);
			$this->txt_preview = str_replace("\n", "%0A", $body);
			
			// layout
			ob_start();
			require($txt_layout_file);
			$this->txt_body = ob_get_contents();
			ob_end_clean();
			
			//
			// html
			//
			
			// template
			ob_start();
			require($html_file);
			$body = ob_get_contents();
			ob_end_clean();
			
			// get rid of the subject from the body
			$body = substr_replace($body, "", 0, strpos($body, "\n") + 1);
			$this->html_preview = $body;
			
			// layout
			ob_start();
			require($html_layout_file);
			$this->html_body = ob_get_contents();
			ob_end_clean();
			
		}
		
		
		
		/**
		 * connect function
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		private function connect()
		{
			if(MAIL_TYPE == "SMTP")
			{
				$this->connection =& new Swift_Connection_SMTP(MAIL_SERVER, Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS);
				$this->connection->setUsername(MAIL_USER);
				$this->connection->setpassword(MAIL_PASSWORD);
			}
			
			else if (MAIL_TYPE == "Sendmail")
			{
				$this->connection =& new Swift_Connection_Sendmail(Swift_Connection_Sendmail::AUTO_DETECT);
			}
			
			else
			{
				$this->connection = new Swift_Connection_NativeMail();
			}
		}
		
		
		/**
		 * send function
		 *
		 * @param boolean $to_one 
		 * @return void
		 * @author Brian Bal
		 */
		
		public function send($to_one = true)
		{
			if(! $this->is_rendered)
			{
				$this->render();
			}
			
			if($this->connection === false)
			{
				$this->connect();
			}
			
			try 
			{
				$swift =& new Swift($this->connection);
				
				//Create a message
				$message =& new Swift_Message($this->subject);
				
				//Add some "parts"
				$message->attach(new Swift_Message_Part($this->txt_body));
				$message->attach(new Swift_Message_Part($this->html_body, "text/html"));
				
				
				
				foreach($this->to as $to)
				{
					$swift->send($message, new Swift_Address($to['email'], $to['name']), new Swift_Address($this->from['email'], $this->from['name']));
					
					PkCore::log("mailer", "Email Sent (" . $to['email'] . ")\n");
				}
				
				//It's polite to do this when you're finished
				$swift->disconnect();
			}
			catch (Swift_ConnectionException $e)
			{
				PkCore::log("mailer", "PkMailer Connection Error (" . $e->getMessage() . ")\n");
			}
			catch (Swift_Message_MimeException $e)
			{
				PkCore::log("mailer", "PkMailer Mime Error (" . $e->getMessage() . ")\n");
			}
		}
		
		
		
		/**
		 * preview function
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		public function preview()
		{
			if(! $this->is_rendered)
			{
				$this->render();
			}
			
			return array("subject" => $this->subject, "txt_body" => $this->txt_preview, "html_body" => $this->html_preview);
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * href function
		 *
		 * returns a string with the link that is needed
		 * this is a specialized version of the PkView
		 * function to make it easier for emails
		 *
		 * @param string $controller_name 
		 * @param string $action_name 
		 * @param string $id 
		 * @param string $full_url 
		 * @return void
		 * @author Brian Bal
		 */
		
		function href($controller_name, $action_name = null, $id = null, $full_url = true)
		{
			$uri = $controller_name;
			if($action_name != null){ $uri .= "/$action_name"; }
			if($id != null){ $uri .= "/$id"; }
			$uri = WWW_DIR . "$uri";
			
			if($full_url)
			{
				if($full_url === "https")
				{
					$uri = "https://" . SERVER_NAME . $uri;
				}else{
					$uri = "http://" . SERVER_NAME . $uri;
				}
			}
			
			return $uri;
		}
		
		
		
		/**
		 * link_to function
		 *
		 * @param string $name 
		 * @param string $options 
		 * @param string $html_options 
		 * @return void
		 * @author Brian Bal
		 */
		
		function link_to($name, $options = null, $html_options = null)
		{
			if($options == null){ $options = array(); }
			if($html_options == null){ $html_options = array(); }
			
			$anchor = "<a ";
			
			foreach($html_options as $key => $value)
			{
				$anchor = $anchor . "$key=\"$value\"";
			}
			
			$anchor = $anchor . " href=\"";
			
			if(isset($options['full_url']))
			{
				if($options['full_url'])
				{
					$anchor .= "http://" . SERVER_NAME;
				}
			}else{
				if(! isset($options['url']))
				{
					$anchor .= "http://" . SERVER_NAME;
				}
			}
			
			// add the link options
			foreach($options as $key => $value)
			{
				if($key == "controller")
				{
					$anchor = $anchor . WWW_DIR . $value;
				}
				else if($key == "action" || $key == "id")
				{
					$anchor = $anchor . "/" . $value;
				}
				else if($key == "url")
				{
					$anchor = $anchor . "http://" . $value;
				}
				else{
					$anchor = $anchor . $key . "/" . $value;
				}
			}
			$anchor = $anchor . "\"";

			$anchor = $anchor . ">$name</a>";

			echo $anchor;
		}
		
	}
	
	
	